package com.ruanhb.wsp.job;

import com.ruanhb.wsp.service.CityClient;
import com.ruanhb.wsp.service.WeatherDataCollectionService;
import com.ruanhb.wsp.vo.City;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;


/**
 * Created by r on 2018-9-21
 */
@Slf4j
public class WeatherDataSyncJob extends QuartzJobBean {

    @Autowired
    private WeatherDataCollectionService weatherDataCollectionService;

    @Autowired
    private CityClient cityClient;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("Weather Data Sync Job. Start!");

        //获取城市ID列表
        List<City> cityList = null;
        try {
            cityList = cityClient.listCity();
        } catch (Exception e) {
            log.error("城市同步异常【{}】", e);
        }

        //遍历城市ID获取天气
        if (cityList != null) {
            for (City city : cityList) {
                String cityId = city.getCityId();
                log.info("Weather Data Sync Job. cityId={}", cityId);
                weatherDataCollectionService.syncDateByCityId(cityId);
            }
        }

        log.info("Weather Data Sync Job. End!");
    }
}
